<template>
  <div class="container-directorys">
    <el-card>
      <!-- 面包屑导航 -->
      <div slot="header" v-if="subject.id && subject.name">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{ subject.name }}</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- /面包屑导航 -->
      <!--头部搜索框-->
      <el-row>
        <el-col :span="18">
          <!-- 表单 -->
          <el-form label-width="80px" inline size="small" class="directiorys-form">
            <!-- 目录搜索 -->
            <el-form-item label="目录名称">
              <el-input placeholder="" v-model="reqParams.directoryName"></el-input>
            </el-form-item>
            <!-- /目录搜索 -->
            <!-- 状态搜索 -->
            <el-form-item label="状态">
              <!-- <el-input placeholder="搜索状态" ></el-input> -->
              <el-select v-model="reqParams.state" placeholder="请选择">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
              </el-select>
            </el-form-item>
            <!-- /状态搜索 -->
            <!-- 按钮 -->
            <el-form-item>
              <el-button @click="onClear">清除</el-button>
              <el-button type="primary" @click="onSubmit">搜索</el-button>
            </el-form-item>
            <!-- /按钮 -->
          </el-form>
          <!-- 表单 -->
        </el-col>
        <!-- 新增目录 -->
        <el-col :span="6" style="text-align: right">
          <el-button v-if="subject.id && subject.name" type="text" icon="el-icon-back" @click="$router.back()">返回学科</el-button>
          <el-button type="success" size="small" icon="el-icon-edit" @click="openDialog()">新增目录</el-button>
        </el-col>
        <!-- /新增目录 -->
      </el-row>
      <!-- /头部搜索框 -->
      <!-- 信息提示 -->
      <el-alert :title="`数据一共${total}条`" type="info" show-icon :closable="false" style="margin-bottom: 15px"> </el-alert>
      <!-- /信息提示 -->
      <!-- 表格信息 -->
      <el-table :data="dictorysList" style="width: 100%">
        <el-table-column type="index" label="序号"> </el-table-column>
        <el-table-column prop="subjectName" label="所需学科"> </el-table-column>
        <el-table-column prop="directoryName" label="目录名称"> </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column label="创建日期">
          <template slot-scope="scope">
            {{ scope.row.addDate | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column prop="totals" label="面试题数量"> </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            {{ scope.row.state ? "已启用" : "已禁用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-link type="primary" :underline="false" @click="changeState(scope.row.id, scope.row.state)">
              {{ scope.row.state == 1 ? "禁用" : "启用" }}
            </el-link>
            <el-link :type="scope.row.state ? 'info' : 'primary'" :underline="false" :disabled="scope.row.state ? true : false">修改</el-link>
            <el-link :type="scope.row.state ? 'info' : 'primary'" :underline="false" :disabled="scope.row.state ? true : false" @click="delDirectory(scope.row)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- /表格信息 -->
      <!-- 分页按钮 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="reqParams.page" :page-size="reqParams.pagesize" :page-sizes="[5, 10, 20]" layout="prev,pager,next,sizes,jumper" class="direc-pagination" :total="total"> </el-pagination>
      <!-- /分页按钮 -->
    </el-card>
    <!-- 添加|修改 -->
    <directorys-add ref="directorysAdd" :data="currDirectory" @updateList="updateList()"></directorys-add>
  </div>
</template>

<script>
// 导入方法
import { list as getDictorysList, changeState, remove } from "@/api/hmmm/directorys";
import DirectorysAdd from "@/module-hmmm/components/directorys-add";
export default {
  name: "pageDirectorys",
  data() {
    return {
      reqParams: {
        subjectName: null, // 学科名
        page: 1,
        pagesize: 10, // 单页多少
        state: null,
      },
      currDirectory: {},
      dictorysList: [],
      total: 0,
      // 当前页
      options: [
        {
          value: "0",
          label: "禁用",
        },
        {
          value: "1",
          label: "启用",
        },
      ],
    };
  },
  computed: {
    subject() {
      return this.$route.query || {};
    },
  },
  watch: {
    "$route.query": function () {
      this.getDictorys();
    },
  },
  components: {
    DirectorysAdd,
  },
  created() {
    this.getDictorys();
  },
  methods: {
    // 数据获取
    async getDictorys() {
      const res = await getDictorysList(this.reqParams);
      console.log(res);
      this.dictorysList = res.data.items;
      this.total = res.data.counts;
      console.log(this.reqParams);
    },
    // 清除表单
    onClear() {
      console.log("清除");
      this.reqParams.subjectName = null;
      this.reqParams.state = null;
    },
    // 搜索
    onSubmit() {
      console.log("搜索", this.reqParams);
      this.reqParams.page = 1;
      this.getDictorys();
    },
    // 改变表格尺码
    handleSizeChange(newSize) {
      console.log(newSize);
      this.reqParams.pagesize = newSize;
      this.reqParams.page = 1;
      this.getDictorys();
    },
    // 翻页
    handleCurrentChange(newPage) {
      this.reqParams.page = newPage;
      this.getDictorys();
    },
    // 禁止状态切换按钮
    async changeState(id, state) {
      // 如果是1 ，点击改为0，同时修改和删除变为禁用状态
      console.log("禁止", state);
      if (state == 1) {
        state = 0;
      } else {
        state = 1;
      }
      await changeState({
        id,
        state,
      });
      this.getDictorys();
      // console.log("禁止", state);
    },

    // 删除
    delDirectory(directory) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await remove(directory);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getDictorys();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 打开弹出框
    openDialog(directory = {}) {
      this.currDirectory = directory;
      this.$nextTick(() => {
        this.$refs.directorysAdd.open();
      });
    },
    // 新增|修改 后更新列表
    updateList() {
      if (!this.currDirectory.id) {
        this.reqParams.page = 1;
      }
      this.getDictorys();
    },
    // 页面跳转
  },
};
</script>

<style scoped lang="scss">
.container-directorys {
  padding: 10px 15px;
  .direc-pagination {
    float: right;
    margin-top: 15px;
    margin-bottom: 15px;
  }
}
.el-link {
  margin-right: 10px;
}
</style>
